package com.whq.thrift.api.web.admin.db.admin;

import java.util.Map;

import org.apache.ibatis.jdbc.SqlBuilder;

import com.whq.common.StringUtil;

public class PermissionUriSqlProvider {
	public String getSelectRecordSql(Map<String, Object> parameter) {
		SqlBuilder.BEGIN();
		SqlBuilder.SELECT("*");
		SqlBuilder.FROM("t_permission_uri");
		if (parameter.get("permissionId") != null) {
			SqlBuilder.WHERE("permission_id=#{permissionId}");
		}

		if (parameter.get("uri") != null) {
			SqlBuilder.WHERE("uri=#{uri}");
		}
		
		if (parameter.get("module") != null) {
			SqlBuilder.WHERE("module=#{module}");
		}

		SqlBuilder.ORDER_BY("permission_id asc");
		return SqlBuilder.SQL() + " limit #{beginIndex}, #{maxCount} ";
	}

	public String getSelectCountSql(Map<String, Object> parameter) {
		SqlBuilder.BEGIN();
		SqlBuilder.SELECT("count(*)");
		SqlBuilder.FROM("t_permission_uri");

		if ((parameter.get("permissionId") != null) && (StringUtil.isNotBlank(parameter.get("permissionId").toString()))) {
			SqlBuilder.WHERE("permission_id like #{permissionId}");
		}

		if (parameter.get("uri") != null) {
			SqlBuilder.WHERE("uri=#{uri}");
		}
		
		if (parameter.get("module") != null) {
			SqlBuilder.WHERE("module=#{module}");
		}

		return SqlBuilder.SQL();
	}

	public String getUpdateSql(PermissionUri permissionUri) {
		SqlBuilder.BEGIN();
		SqlBuilder.UPDATE("t_permission_uri");

		if (StringUtil.isNotBlank(permissionUri.getUri())) {
			SqlBuilder.SET("uri = #{uri}");
		}

		SqlBuilder.SET("module = #{module}");

		SqlBuilder.SET("comment = #{comment}");

		if (permissionUri.getUpdateTime() != null) {
			SqlBuilder.SET("update_time=#{updateTime}");
		}

		SqlBuilder.WHERE("permission_id=#{permissionId}");
		return SqlBuilder.SQL();
	}
}
